package com.vend.common.db.commondata;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;
import com.vend.common.VendBaseApplication;

import java.util.Map;

/**
 * Created by glendal on 2018/4/16.
 * SharedPreferences 帮助类
 */
@SuppressLint("ApplySharedPref")
public class SPHelper {
    private static final SPHelper mShareHelp = new SPHelper();
    private SharedPreferences sp;

    public static SPHelper instance() {
        return mShareHelp;
    }
    private SharedPreferences CheckSharedPreferences() {
        if (sp == null) {
            if (VendBaseApplication.Companion.instance() != null) {
                String SP_FILE_NAME = "SPECIAL_SITUATION_SP";
                sp = VendBaseApplication.Companion.instance().getSharedPreferences(SP_FILE_NAME,
                        Context.MODE_PRIVATE);
            }

        }
        return sp;
    }

    public void putData(Map<String, String> datas) {
        SharedPreferences.Editor editor = CheckSharedPreferences().edit();
        for (Map.Entry<String, String> entry : datas.entrySet()) {
            editor.putString(entry.getKey(), entry.getValue());
        }
        editor.commit();
    }

    public boolean putData(String key, String value) {
        SharedPreferences.Editor editor = CheckSharedPreferences().edit();
        editor.putString(key, value);
        return editor.commit();
    }

    public String getValue(String key) {
        SharedPreferences sp = CheckSharedPreferences();
        if (sp == null) {
            return null;
        }
        return sp.getString(key, "");
    }

    public long getLongValue(String key) {
        SharedPreferences sp = CheckSharedPreferences();
        if (sp == null) {
            return -1;
        }
        return sp.getLong(key, -1);
    }

    public void putLongValue(String key, long value) {
        SharedPreferences.Editor editor = CheckSharedPreferences().edit();
        editor.putLong(key, value);
        editor.commit();
    }



    /**
     * 如果没有该Key返回false
     *
     * @param key
     * @return
     */
    public boolean getBooleanValue(String key) {
        SharedPreferences sp = CheckSharedPreferences();
        if (sp == null) {
            return false;
        }
        return sp.getBoolean(key, false);
    }
    /**
     * 如果没有该Key返回false
     *
     * @param key
     * @return
     */
    public boolean getBooleanValueTrue(String key) {
        SharedPreferences sp = CheckSharedPreferences();
        if (sp == null) {
            return true;
        }
        return sp.getBoolean(key, true);
    }

    /**
     * 如果没有该Key返回false
     *
     * @param key
     * @param def 默认
     * @return
     */
    public boolean getBooleanValue(String key, boolean def) {
        SharedPreferences sp = CheckSharedPreferences();
        if (sp == null) {
            return false;
        }
        return sp.getBoolean(key, def);
    }


    public void putBooleanValue(String key, boolean value) {
        SharedPreferences.Editor editor = CheckSharedPreferences().edit();
        editor.putBoolean(key, value);

        editor.commit();
    }

    public int getIntValue(String key, int def) {
        SharedPreferences sp = CheckSharedPreferences();
        if (sp == null) {
            return -1;
        }
        return sp.getInt(key, def);
    }

    public void putIntValue(String key, int value) {
        SharedPreferences.Editor editor = CheckSharedPreferences().edit();
        editor.putInt(key, value);
        editor.commit();
    }
    public int getIntValueObuPower(String key, int def) {
        SharedPreferences sp = CheckSharedPreferences();
        if (sp == null) {
            return 31;
        }
        return sp.getInt(key, def);
    }
    public void putDataBoolean(String key, boolean value) {
        SharedPreferences.Editor editor = CheckSharedPreferences().edit();
        editor.putBoolean(key, value);

        editor.commit();
    }
    public boolean getValueBoolean(String key) {
        SharedPreferences sp = CheckSharedPreferences();
        if (sp == null) {
            return false;
        }
        boolean value = sp.getBoolean(key, false);
        return value;
    }

    /**
     * 如果没有该Key返回false
     * @param key 键
     * @return boolean 这里安防开启默认是true
     */
    public boolean getBooleanValueFalse(String key) {
        SharedPreferences sp = CheckSharedPreferences();
        if (sp == null) {
            return false;
        }
        return sp.getBoolean(key, false);
    }
    public int getIntValue(String key){
        SharedPreferences sp = CheckSharedPreferences();
        if (sp == null){
            return 0;
        }
        return sp.getInt(key,0);
    }
    public int getIntObuPowerDefault(String key){
        SharedPreferences sp = CheckSharedPreferences();
        if (sp == null){
            return 0;
        }
        return 1;
//        return sp.getInt(key, Factory.INSTANCE.getOBUAPI().getPowerList().get(0));
    }
    public void setIntValue(String key, int value){
        SharedPreferences.Editor editor = CheckSharedPreferences().edit();
        editor.putInt(key, value);

        editor.commit();
    }
}
